package com.imooc.dom4j;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XPathTestor {
	public void xpath(String xpathExp) {
		//读取XML文件
		String file = "D:\\pro_software\\eclipse-workspace\\xml\\src\\com\\imooc\\dom4j\\hrTest.xml";
		//SAXReader类是读取XML文件的核心类，用于将XML解析后以树的形式保存到内存中
		SAXReader reader = new SAXReader();
		
		try {
			Document document = reader.read(file);
			//node是Element的父类
			List<Node> nodes = document.selectNodes(xpathExp);
			for(Node node : nodes) {
				Element emp = (Element) node;
				System.out.println(emp.attributeValue("no"));
				System.out.println(emp.elementText("name"));
				System.out.println(emp.elementText("age"));
				System.out.println(emp.elementText("salary"));
				
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	public static void main(String[] args) {
		XPathTestor testor = new XPathTestor();
		//输出hr节点下的所有employee
		testor.xpath("/hr/employee");
		//testor.xpath("//employee");
		//testor.xpath("//employee[1]");
		//testor.xpath("//employee[last()]");
		
	}
}	
